package io.renren.modules.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.javafx.collections.MappingChange;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.ApproveDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.service.ApproveService;
import io.renren.modules.app.utils.SqlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.*;
@Service("approveService")
public class ApproveServiceImpl implements ApproveService {

    @Autowired
    private ApproveDao approveDao;

    @Autowired
    private SqlUtils sqlUtils;

    @Value("${billTableConfig.list}")
    private String billTableConfigStr;

    public IPage<Map<String, Object>> getTaskList(Map<String, Object> data ){
        JSONObject billTableConfig = JSONObject.parseObject(billTableConfigStr);
        String billType = data.get("billtype").toString();
        String statusCode = data.get("statuscode").toString();
        if("handled".equals(statusCode)){
            data.put("approvestatus",1);//已处理
        }else{
            data.put("approvestatus",0);//未处理
        }
        Object billTable= billTableConfig.get(billType);
        IPage<Map<String, Object>> page = new Query<Map<String, Object>>().getPage(data,"senddate",false) ;
        return approveDao.queryTasks(page, data,billTable);
    }

    public  IPage<Map<String, Object>> getTaskListTest(Map<String, Object> data ) throws IOException {
        JSONObject billTableConfig = JSONObject.parseObject(billTableConfigStr);
        String billType = data.get("billtype").toString();
        String statusCode = data.get("statuscode").toString();
        if("handled".equals(statusCode)){
            data.put("approvestatus",1);//已处理
        }else{
            data.put("approvestatus",0);//未处理
        }
        Object billTable= billTableConfig.get(billType);
        IPage<Map<String, Object>> page = new Query<Map<String, Object>>().getPage(data,"senddate",false) ;
        IPage pageData = approveDao.queryTaskKeys(page, data,billTable);
        List keysData= pageData.getRecords();
        String sql = sqlUtils.builder(billType);
        String keysStr = "'"+String.join("','", keysData)+"'";
        String sqlStr=String.format(sql, keysStr);
        List taskData = approveDao.queryTasksTest(sqlStr);
        pageData.setRecords(taskData);
        return pageData;
    }

}
